//
//  ViewController.m
//  图片浏览器
//
//  Created by zxy on 2018/10/22.
//  Copyright © 2018年 zxy. All rights reserved.
//

#import "ViewController.h"
#import "MainModel.h"
#import "MainCell.h"
#import "NetWork/PPNetworkHelper.h"
#import "LookBigPictureController.h"
#define Home_url @"http://open.qyer.com/qyer/home/home_feed?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&lat=22.62593436777694&lon=114.0697465898473&page=1&track_app_channel=App%2520Store&track_app_version=7.4&track_device_info=iPhone%25206&track_deviceid=7B2E86D8-EB88-4036-B818-9EF729EF7FCB&track_os=ios%25209.3.1&v=1"
static NSString *const MainCellId = @"MainCellId";
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tabView;
@property(nonatomic,strong)NSMutableArray *dataSoure;
@end

@implementation ViewController

-(NSMutableArray *)dataSoure{
    if(!_dataSoure){
        _dataSoure = [NSMutableArray array];
    }
    return _dataSoure;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabView registerNib:[UINib nibWithNibName:NSStringFromClass([MainCell class]) bundle:nil] forCellReuseIdentifier:MainCellId];
    [self requestDatas];
    if (@available(iOS 11.0, *)) {
        self.tabView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

-(void)requestDatas{
    [PPNetworkHelper GET:Home_url parameters:nil success:^(id responseObject) {
        NSArray *array = responseObject[@"data"][@"feed"][@"entry"];
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MainModel *model = [MainModel mj_objectWithKeyValues:obj];
            [self.dataSoure addObject:model];
        }];
        [self.tabView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"error=%@",error);
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSoure.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MainCell *cell = [tableView dequeueReusableCellWithIdentifier:MainCellId forIndexPath:indexPath];
    if(self.dataSoure.count){
        cell.model = self.dataSoure[indexPath.row];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LookBigPictureController *ctl = [[LookBigPictureController alloc]init];
    ctl.model = self.dataSoure[indexPath.row];
    [self presentViewController:ctl animated:YES completion:nil];
}
@end
